#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    serialPortReader = new SerialPortReader(this);
//    serialPortReader->startReading();

    serialPort = new QSerialPort(this);

    serialPort->setPortName("/dev/ttyUSB0"); // 修改为你实际使用的串口号

    serialPort->setBaudRate(QSerialPort::Baud9600);
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);

    if (!serialPort->open(QIODevice::ReadWrite)) {
        qDebug() << "Failed to open port" << serialPort->portName() << ", error:" << serialPort->errorString();
        return;
    }

    qDebug() << "Serial port opened.";



//    connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::handleReadyRead);
    connect(serialPort, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::handleReadyRead() {
    QByteArray data = serialPort->readAll();
    qDebug() << "Data received:" << data;
}

